package com.teamcenter.rac.massupdate.ui.grid;

import org.apache.log4j.Logger;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

import com.teamcenter.rac.common.Activator;
import com.teamcenter.rac.common.PropertyValueRenderer;
import com.teamcenter.rac.common.TCTypeRenderer;
import com.teamcenter.rac.common.propertyicon.IPropertyIconService;
import com.teamcenter.rac.kernel.IPropertyName;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.massupdate.ui.grid.layers.MassUpdateBodyLayerStack;
import com.teamcenter.rac.massupdate.utils.ImageRegistryManager;
import com.teamcenter.rac.util.ImageUtilities;
import com.teamcenter.rac.util.OSGIUtil;

import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.layer.ILayer;
import net.sourceforge.nattable.layer.cell.LayerCell;
import net.sourceforge.nattable.painter.cell.ImagePainter;
@SuppressWarnings({"rawtypes"})
public abstract class AbstractMassUpdateCellImagePainter extends ImagePainter {
	private final String m_propertyName;
	private final String m_referencePropertyName;
	private final String m_iconSize;
	private ImageRegistryManager m_registryManager;
	private IPropertyIconService m_renderIconObjectService;

	public AbstractMassUpdateCellImagePainter(String paramString1, String paramString2) {
		this(null, paramString1, paramString2);
	}

	public AbstractMassUpdateCellImagePainter(String paramString1, String paramString2, String paramString3) {
		this.m_referencePropertyName = paramString1;
		this.m_propertyName = paramString2;
		this.m_iconSize = paramString3;
		this.m_renderIconObjectService = ((IPropertyIconService) OSGIUtil.getService(Activator.getDefault(),
				IPropertyIconService.class));
		this.m_registryManager = ImageRegistryManager.getInstance();
	}

	public void setRenderIconObjectService(IPropertyIconService paramIPropertyIconService) {
		this.m_renderIconObjectService = paramIPropertyIconService;
	}

	protected Image getImage(LayerCell paramLayerCell, IConfigRegistry paramIConfigRegistry) {
		ILayer localILayer = paramLayerCell.getLayer();
		int i = 0;
		MassUpdateBodyLayerStack localMassUpdateBodyLayerStack = null;
		while (i == 0) {
			if (localILayer == null) {
				break;
			}
			if ((localILayer instanceof MassUpdateBodyLayerStack)) {
				i = 1;
				localMassUpdateBodyLayerStack = (MassUpdateBodyLayerStack) localILayer;
				break;
			}
			localILayer = localILayer.getUnderlyingLayerByPosition(paramLayerCell.getColumnPosition(),
					paramLayerCell.getRowPosition());
		}
		if (localMassUpdateBodyLayerStack == null) {
			return null;
		}
		Object localObject1 = localMassUpdateBodyLayerStack.getDataProvider()
				.getRowObject(paramLayerCell.getRowIndex());
		TCComponent localTCComponent = getTCComponentFromRowObject(localObject1);
		if (localTCComponent == null) {
			return null;
		}
		if (this.m_referencePropertyName != null) {
			try {
				localTCComponent = localTCComponent.getReferenceProperty(this.m_referencePropertyName);
			} catch (TCException localTCException) {
				Logger.getLogger(ImpactedObjectCellImagePainter.class).error(localTCException.getLocalizedMessage(),
						localTCException);
			}
		}
		Image localImage = null;
		Object localObject2;
		if (("release_status_list".equals(this.m_propertyName))
				|| (IPropertyName.RELEASE_STATUSES.equals(this.m_propertyName))) {
			localObject2 = (String) paramLayerCell.getDataValue();
			localImage = this.m_registryManager.getImageFromRegistry((String) localObject2);
			if (localImage != null) {
				return localImage;
			}
			localImage = loadReleaseStatusImage((String) localObject2);
			this.m_registryManager.putImageInRegistry((String) localObject2, localImage);
		} else {
			localImage = this.m_registryManager.getImageFromRegistry(localTCComponent, this.m_propertyName);
			if (localImage != null) {
				return localImage;
			}
			if ("object_string".equals(this.m_propertyName)) {
				localImage = TCTypeRenderer.getImage(localTCComponent);
			} else {
				localObject2 = this.m_renderIconObjectService.getRenderedComponentIcon(localTCComponent,
						this.m_propertyName, false, this.m_iconSize);
				ImageDescriptor localImageDescriptor = ImageUtilities.getImageDescriptor(localObject2);
				if (localImageDescriptor != null) {
					localImage = localImageDescriptor.createImage();
				}
			}
			this.m_registryManager.putImageInRegistry(localTCComponent, this.m_propertyName, localImage);
		}
		return localImage;
	}

	private Image loadReleaseStatusImage(String paramString) {
		if ((paramString == null) || (paramString.isEmpty())) {
			return null;
		}
		int i = paramString.lastIndexOf(',');
		if (i >= 0) {
			paramString = paramString.substring(i + 1).trim();
		}
		Image localImage = PropertyValueRenderer.r.getImage(this.m_propertyName + "." + paramString.trim() + ".ICON");
		return localImage == null ? PropertyValueRenderer.r.getImage(this.m_propertyName + ".AnyValue.ICON")
				: localImage;
	}

	protected abstract TCComponent getTCComponentFromRowObject(Object paramObject);
}
